﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AceMobile.Models.Phones;
using AceMobile.Models;

namespace AceMobile.Controllers.Admin
{
    [Authorize(Roles = "Administrator,Phonemanager")]
    public class PhoneAdminController : Controller
    {
        private MobileWebContext db = new MobileWebContext();

        //
        // GET: /PhoneAdmin/

        public ViewResult Index()
        {
            return View(db.Phones.Include("PhoneOS").Include("PhoneProducer").ToList());
        }

        //
        // GET: /PhoneAdmin/Details/5

        public ViewResult Details(int id)
        {
            Phone phone = db.Phones.Find(id);
            return View(phone);
        }

        //
        // GET: /PhoneAdmin/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /PhoneAdmin/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Phone phone, int Producer, int OS)
        {            
            if (ModelState.IsValid)
            {
                phone.PhoneProducer = db.PhoneProducers.Find(Producer);
                phone.SumAmount = phone.Amount;
                phone.PhoneOS = db.PhoneOSs.Find(OS);

                db.Phones.Add(phone);
                db.SaveChanges();

                return RedirectToAction("Index");  
            }

            return View(phone);
        }
        
        //
        // GET: /PhoneAdmin/Edit/5
 
        public ActionResult Edit(int id)
        {
            Phone phone = db.Phones.Find(id);
            ViewBag.PhoneOSID = new SelectList(db.PhoneOSs, "ID", "Name", phone.PhoneOS.ID);
            ViewBag.PhoneProducerID = new SelectList(db.PhoneProducers, "ID", "Name", phone.PhoneProducer.ID);
            return View(phone);
        }

        //
        // POST: /PhoneAdmin/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Phone phone)
        {
            if (ModelState.IsValid)
            {
                db.Entry(phone).State = EntityState.Modified;
                //db.Entry(phone).Reload();                                
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PhoneOSID = new SelectList(db.PhoneOSs, "ID", "Name", phone.PhoneOSID);
            ViewBag.PhoneProducerID = new SelectList(db.PhoneProducers, "ID", "Name", phone.PhoneProducerID);
            return View();
        }        

        [HttpPost]        
        public ActionResult Delete(int id)
        {
            try
            {
                Phone phone = db.Phones.Find(id);
                db.Phones.Remove(phone);
                db.SaveChanges();
                return Json(new { status = "Success" });
            }
            catch (Exception)
            {
                return Json(new { status = "NotSuccess" });
            }                        
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}